﻿package com.neriksworkshop.lib.colourLovers.items
{
	import com.neriksworkshop.lib.colourLovers.core.AbstractGraphicItem;
	import com.neriksworkshop.lib.colourLovers.params.Params;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
		
	/**
	 * ...
	 * @author ...
	 */
	public class Palette extends AbstractGraphicItem implements IGraphicItem
	{		
			
		//from colourlovers api
		private var _colors:/*Number*/Array = new Array();
		
		//computed
		private var _colorItems:/*Colour*/Array = new Array();
		
		
		
		public function Palette(hexColors:Array = null) 
		{
			super(this);
			
			if (hexColors) 
			{
				_colors = hexColors;
				buildColourItems();
			}
		}
		

		override public function buildFromData(data:*):void
		{
			var dataFinal:* = (data is String) ? XML(data) : data;
			
			super.buildFromData(dataFinal);
						
			var colorsData:* = (dataFinal is XML) ? dataFinal.colors.hex : dataFinal.colors;
			
			_colors = [Number('0x'+colorsData[0]), Number('0x'+colorsData[1]), Number('0x'+colorsData[2]), Number('0x'+colorsData[3]), Number('0x'+colorsData[4])];
					
			buildColourItems();			
		}
		
		/*
		public override function buildFromXML(xmlData:XML):void
		{
			super.buildFromXML(xmlData);
			
			_colors = [Number('0x'+xmlData.colors.hex[0]), Number('0x'+xmlData.colors.hex[1]), Number('0x'+xmlData.colors.hex[2]), Number('0x'+xmlData.colors.hex[3]), Number('0x'+xmlData.colors.hex[4])];
					
			buildColourItems();
			
		}
		
		public override function buildFromJSON(jsonData:Object):void
		{
			super.buildFromJSON(jsonData);
			
			_colors = [Number('0x'+jsonData.colors.hex[0]), Number('0x'+jsonData.colors.hex[1]), Number('0x'+jsonData.colors.hex[2]), Number('0x'+jsonData.colors.hex[3]), Number('0x'+jsonData.colors.hex[4])];
					
			buildColourItems();
		}*/		
		
		
		
		public override function createSprite(dimensions:Rectangle = null):Sprite
		{
			if (!dimensions) var dimensions:Rectangle = Params.DEFAULT_DIMENSIONS;
			
			_sp = new Sprite();
			
			var subSpX:Number = dimensions.x;
			
			for (var i:int = 0; i < 5; i++) 
			{
				//TODO implement colorwidths
				var subSp:Sprite = new Sprite();
				_sp.addChild(subSp);
				subSp.graphics.beginFill(_colors[i]);
				subSp.graphics.drawRect(0, 0, dimensions.width / 5, dimensions.height);
				subSp.y = dimensions.y;
				subSp.x = subSpX;
				subSpX += dimensions.width / 5;
			}
						
			return _sp;
		}		
		
		
		
						
		public function get colors():Array  							{ 	return _colors;  }
		
		public function get colorItems():/*Color*/Array 				{ 	return _colorItems; }

		
		public function getColorIndexInPalette(hexStr:String):int
		{
			var s:String = hexStr.toLowerCase();
			for (var i:int = 0; i < 5; i++) 
			{
				//trace(hexStr, _colors[i])
				if (s == _colors[i].toLowerCase()) return i;
			}
			return -1;
		}
		
		
		private function buildColourItems():void
		{
			_colorItems = new Array();
			
			for each (var color:String in colors) 
			{
				_colorItems.push(new Color(Number('0x'+color)));
			}
		}		

	}
	
}